package store.dao;

import store.po.User;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class UserDAO {
    public User findByLoginName(String loginName) throws Exception {
        User u=null;
        Connection con= null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        try {
            Class.forName(DB.JDBC_DRIVER);
            con= DriverManager.getConnection(DB.JDBC_URL,DB.JDBC_USER,DB.JDBC_PASSWORD);
            ps=con.prepareStatement("select * from t_user where loginname=? ");
            ps.setString(1,loginName);
            rs=ps.executeQuery();
            if (rs.next()){
                u=new User();
                u.setId(rs.getInt("id"));
                u.setLoginName(rs.getString("loginname"));
                u.setPwd(rs.getString("pwd"));

            }
        } catch (Exception e) {
            e.printStackTrace();
            throw new Exception("数据库异常:"+e.getMessage());
        }finally {
            if(rs!=null) rs.close();
            if(ps!=null) ps.close();
            if(con!=null) con.close();
        }
        return u;
    }
}
